﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 折叠菜单
    /// </summary>
    public class ExpanderMenu : Menu
    {
        static ExpanderMenu()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ExpanderMenu), new FrameworkPropertyMetadata(typeof(ExpanderMenu)));
        }
        ControlTemplate menu_level0; 
        public ExpanderMenu()
        {
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            menu_level0 = this.FindResource("menu_level0") as ControlTemplate; 
            this.resetTemplate();
        }

        protected override void OnStyleChanged(Style oldStyle, Style newStyle)
        {
            base.OnStyleChanged(oldStyle, newStyle);
        }

        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
            if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                resetTemplate();
            }
        }

        private void resetTemplate()
        {
            if (this.Items != null)
            {
                foreach (var item in this.Items)
                {
                    if (item is MenuItem mi)
                    {
                        SetLevel(mi, 0);
                        if (mi.Template != menu_level0)
                        {
                            mi.Template = menu_level0;
                        }
                    }

                }
            }
        }

         
        public static Thickness GetInnerMargin(DependencyObject obj)
        {
            return (Thickness)obj.GetValue(InnerMarginProperty);
        }

        public static void SetInnerMargin(DependencyObject obj, Thickness value)
        {
            obj.SetValue(InnerMarginProperty, value);
        }

        public static readonly DependencyProperty InnerMarginProperty =
            DependencyProperty.RegisterAttached("InnerMargin", typeof(Thickness), typeof(ExpanderMenu), new PropertyMetadata(new Thickness(45, 0, 0, 0)));




        public static int GetLevel(DependencyObject obj)
        {
            return (int)obj.GetValue(LevelProperty);
        }

        public static void SetLevel(DependencyObject obj, int value)
        {
            obj.SetValue(LevelProperty, value);
        }
        public static readonly DependencyProperty LevelProperty =
            DependencyProperty.RegisterAttached("Level", typeof(int), typeof(ExpanderMenu), new PropertyMetadata(0));





        public static bool GetIsExpanded(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsExpandedProperty);
        }

        public static void SetIsExpanded(DependencyObject obj, bool value)
        {
            obj.SetValue(IsExpandedProperty, value);
        }

        public static readonly DependencyProperty IsExpandedProperty =
            DependencyProperty.RegisterAttached("IsExpanded", typeof(bool), typeof(ExpanderMenu), new PropertyMetadata(false));



    }
}
